<?php

/* @version $Id: module_accounts.php,v 1.4 2008-02-14 18:28:23 oleg Exp $ */

/**
 * @see module_base
 */
class module_accounts extends module_base
{
    function module_accounts()
    {
        global $WEB_APP;
        $WEB_APP['title_edit'] = text('txt_edit_user');
        $WEB_APP['title_delete'] = text('txt_delete_users');
        $WEB_APP['title_add'] = text('txt_add_user');
    }

    function get_post_user()
    {
        global $WEB_APP;
        $reg_grants = @unserialize($WEB_APP['settings']['reg_grants']);
        $grants = array();
        if (is_array($reg_grants))
        {
            foreach($reg_grants as $key => $value)
            {
                $grants[$key] = TRUE;
            }
        }

        $user = new user();
        $group = get_group((isset($_POST['group'])) ? trim($_POST['group']) : $WEB_APP['settings']['def_group']);
        $user->group = $group->name;
        $user->login = (isset($_POST['login'])) ? trim($_POST['login']) : '';
        $user->password = (isset($_POST['password'])) ? trim($_POST['password']) : '';
        $user->info = (isset($_POST['info'])) ? trim($_POST['info']) : '';
        $user->mail = (isset($_POST['mail'])) ? trim($_POST['mail']) : '';
        $user->hidden = isset($_POST['hidden']) ? 1 : 0;
        $user->name = (isset($_POST['name'])) ? trim($_POST['name']) : '';
        $user->grants = isset($_POST['grants']) ? $_POST['grants'] : $grants;

        return $user;
    }

    function on_delete()
    {
        global $WEB_APP;
        global $adodb;
        if (!isset($_POST['selected_row']))
        {
            $this->view();
            exit();
        }
        $groups = get_groups('group_name');
        $grants = get_grants('grant_title');
        $user = new user();
        if (is_confirm_delete_action())
        {
            $result = delete_users($_POST['selected_row']);
            if (!$result)
            {
                $WEB_APP['errorstext'] .= text('txt_unpossible_delete_all_users')."<br>";
            }
            $WEB_APP['errorstext'] .= $adodb->ErrorMsg();
            redirect($WEB_APP['errorstext']);
        }
        if (($WEB_APP['action'] == 'delete') &&  (!is_delete_action()))
        {
            $WEB_APP['title'] = $WEB_APP['title_delete'];
            $WEB_APP['items'] = get_users_from_array(array($WEB_APP['id']));
            $WEB_APP['editform'] = FALSE;
            $WEB_APP['list_action'] = 'delete';
        }

        $WEB_APP['title'] = $WEB_APP['title_delete'];
        $WEB_APP['items'] = get_users_from_array($_POST['selected_row']);
        $WEB_APP['editform'] = FALSE;
        $WEB_APP['list_action'] = 'delete';

        $WEB_APP['columns'] = array(new column('id', 'id'),
                new column("user_login", text('txt_login'), "login"),
                new column("group_name", text('txt_group'), "group"),
                new column("user_name", text('txt_user_name'), "name"),
                new column("user_info", text('txt_info'), "info"),
                new column("user_mail", text('txt_mail'), "mail"),
                new column("user_hidden", text('txt_hidden'), "hidden"));

        $WEB_APP['escape'] = TRUE;

        $WEB_APP['submit_title'] = text('txt_delete');


        $WEB_APP['view']->display('list_action.tpl', text('txt_users'));
    }

    function on_confirm_delete()
    {
        global $WEB_APP;
        global $adodb;

        $result = delete_users($_POST['selected_row']);
        if (!$result)
        {
            $WEB_APP['errorstext'] .= text('txt_unpossible_delete_all_users')."<br>";
            $this->view();
            exit();
        }
        $WEB_APP['errorstext'] .= $adodb->ErrorMsg();
        redirect($WEB_APP['errorstext']);
    }

    function on_move()
    {
        global $WEB_APP;
        global $adodb;
        if (!isset($_POST['selected_row']))
        {
            $this->view();
            exit();
        }
        $groups = get_groups('group_name');
        $grants = get_grants('grant_title');
        $user = new user();

        $WEB_APP['title'] = text('txt_move');
        $WEB_APP['items'] = get_users_from_array($_POST['selected_row']);
        $WEB_APP['editform'] = FALSE;
        $WEB_APP['list_action'] = 'move';

        $WEB_APP['columns'] = array(new column('id', 'id'),
                new column("user_login", text('txt_login'), "login"),
                new column("group_name", text('txt_group'), "group"),
                new column("user_name", text('txt_user_name'), "name"),
                new column("user_info", text('txt_info'), "info"),
                new column("user_mail", text('txt_mail'), "mail"),
                new column("user_hidden", text('txt_hidden'), "hidden"));

        $WEB_APP['escape'] = TRUE;

        // Form fields.
        $fields = array();

        $fields[] = new field(  TRUE, text('txt_group'), "select", "group",
                    $user->group, "", $groups, 'id', 'group_name');


        $WEB_APP['fields'] = $fields;
        $WEB_APP['list_action'] = 'move';
        $WEB_APP['submit_title'] = text('txt_move');
        $WEB_APP['view']->display('list_action.tpl', text('txt_users'));
    }

    function on_confirm_move()
    {
        global $WEB_APP;
        global $adodb;
        if ($_POST['group'] == 0)
        {
            $WEB_APP['errorstext'] .= text('txt_insert_group')."<br>";
            $this->on_move();
            exit();
        }
        $result = move_users($_POST['group'], $_POST['selected_row']);
        if (!$result)
        {
            $WEB_APP['errorstext'] .= text('txt_unpossible_move_all_users')."<br>";
        }
        $WEB_APP['errorstext'] .= $adodb->ErrorMsg();
        redirect($WEB_APP['errorstext']);
        $this->view();
    }



    /**
     * Implementation of module_base::view().
     */
    function view()
    {
        global $WEB_APP;
        global $adodb;
        $groups = get_groups('group_name');
        $grants = get_grants('grant_title');
        $user = new user();

        if (is_confirm_delete_action())
        {
            $result = delete_users($_POST['selected_row']);
            if (!$result)
            {
                $WEB_APP['errorstext'] .= text('txt_unpossible_delete_all_users')."<br>";
            }
            $WEB_APP['errorstext'] .= $adodb->ErrorMsg();
            redirect($WEB_APP['errorstext']);
        }
        if (is_confirm_move_action())
        {
            if ($_POST['group'] == 0)
            {
                $WEB_APP['errorstext'] .= text('txt_insert_group')."<br>";
                $this->on_move();
                exit();
            }
            $result = move_users($_POST['group'], $_POST['selected_row']);
            if (!$result)
            {
                $WEB_APP['errorstext'] .= text('txt_unpossible_move_all_users')."<br>";
            }
            $WEB_APP['errorstext'] .= $adodb->ErrorMsg();
            redirect($WEB_APP['errorstext']);
            $this->view();
            exit();
        }
        if (($WEB_APP['action'] == 'delete') &&  (!is_delete_action()))
        {
            $user = get_user($WEB_APP['id']);
            if (!isset($user->id))
            {
                redirect($WEB_APP['errorstext']);
            }

            $WEB_APP['title'] = $WEB_APP['title_delete'];
            $WEB_APP['items'] = get_users_from_array(array($WEB_APP['id']));
            $WEB_APP['editform'] = FALSE;
            $WEB_APP['action'] = 'delete';
        }

        if ($WEB_APP['action'] == 'view')
        {
            // After press Add button.
            if (is_add_form('name'))
            {
                $correct_post = TRUE;
                if ($_POST['group'] == 0)
                {
                    $WEB_APP['errorstext'] .= text('txt_insert_group')."<br>";
                    $correct_post = FALSE;
                }
                if (trim($_POST['login']) == "")
                {
                    $WEB_APP['errorstext'] .= text('txt_insert_login')."<br>";
                    $correct_post = FALSE;
                }
                if (trim($_POST['password']) == "")
                {
                    $WEB_APP['errorstext'] .= text('txt_insert_password')."<br>";
                    $correct_post = FALSE;
                }
                if (trim($_POST['name']) == "")
                {
                    $WEB_APP['errorstext'] .= text('txt_insert_user_name')."<br>";
                    $correct_post = FALSE;
                }

                if  (!(is_email($_POST['mail']) ||
                         ($_POST['mail'] == "")))
                {
                    $WEB_APP['errorstext'] .= text('txt_email_does_not_correct_insert_correct_email')."<br>";
                    $correct_post = FALSE;
                }

                if (get_user_id($_POST['login']) != 0)
                {
                    $WEB_APP['errorstext'] .= text('txt_user_already_exist_insert_another_user_login')."<br>";
                    $correct_post = FALSE;
                }
                if (get_user_id_by_mail($_POST['mail']) != 0)
                {
                    $WEB_APP['errorstext'] .= text('txt_user_mail_already_exist_insert_another_user_mail')."<br>";
                    $correct_post = FALSE;
                }

                if ($correct_post)
                {
                    add_user($_POST['group'],
                             $_POST['login'],
                             $_POST['password'],
                             $_POST['name'],
                             $_POST['info'],
                             $_POST['mail'],
                             isset($_POST['hidden']) ? 1 : 0,
                             isset($_POST['grants']) ? $_POST['grants'] : array());
                    $WEB_APP['errorstext'] .= $adodb->ErrorMsg();
                    redirect($WEB_APP['errorstext']);
                }
            }

            if (is_delete_action())
            {
                $WEB_APP['title'] = $WEB_APP['title_delete'];
                $WEB_APP['items'] = get_users_from_array($_POST['selected_row']);
                $WEB_APP['editform'] = FALSE;
                $WEB_APP['action'] = 'delete';
            }
            else
            {
                $WEB_APP['title'] = text('txt_users');
                // Get sections. On show sections.
                $WEB_APP['items_count'] = get_users_count(new account_filter());//db_count(DB_TABLE_USER);
                // Pages count.
                $pages = get_pages_count($WEB_APP['items_count'], ($WEB_APP['count'] == 0) ? $WEB_APP['items_count'] : $WEB_APP['count']);


                if ($WEB_APP['page'] > $pages)
                {
                    $WEB_APP['page'] = $pages;
                }
                $users = get_users($WEB_APP['sort_field'],
                            $WEB_APP['sort_order'],
                            $WEB_APP['page'],
                            $WEB_APP['count'],
                            new account_filter()
                            );
                $WEB_APP['errorstext'] .= $adodb->ErrorMsg();
                $WEB_APP['items'] = $users;

                $paginator = new paginator($WEB_APP['page'], $pages);
                $paginator->url = $WEB_APP['script_name'];
                $paginator->url_query_array  = $WEB_APP['url_query_array'];
                $WEB_APP['paginator'] = $paginator;
            }
            $user = $this->get_post_user();
        }
        $WEB_APP['row_actions'] = array($WEB_APP['action_edit'], $WEB_APP['action_delete']);
        $WEB_APP['list_actions'] = array($WEB_APP['list_action_delete'], $WEB_APP['list_action_move']);
        $WEB_APP['columns'] = array(new column('id', 'id'),
                new column("user_login", text('txt_login'), "login"),
                new column("group_name", text('txt_group'), "group"),
                new column("user_name", text('txt_user_name'), "name"),
                new column("user_info", text('txt_info'), "info"),
                new column("user_mail", text('txt_mail'), "mail"),
                new column("user_hidden", text('txt_hidden'), "hidden"));

        $WEB_APP['escape'] = TRUE;
        // Form fields.
        $fields = array();
        $fields[] = new field(  TRUE, text('txt_group'), "select", "group",
                        $user->group,"", $groups, "id", "group_name");
        $fields[] = new field(  TRUE, text('txt_login'), "text", "login",
                        $user->login, "");
        $fields[] = new field(  TRUE, text('txt_password'), "text", "password",
                    $user->password, "");
        $fields[] = new field(  TRUE, text('txt_user_name'), "text", "name",
                    $user->name, "");
        $fields[] = new field(  FALSE, text('txt_info'), "textarea", "info",
                    $user->info, "");
        $fields[] = new field(  FALSE, text('txt_mail'), "text", "mail",
                    $user->mail, "");
        $fields[] = new field(  FALSE, text('txt_hidden'), "checkbox", "hidden",
                        $user->hidden == 1, "");

        $tmp = 0;

            $fields[] = new field(  FALSE, text('txt_permissions'), "header");
            foreach($grants as $grant)
            {

                    $fields[] = new field(  FALSE, $grant['grant_title'], "checkbox",
                            "grants[".$grant['id']."]", isset($user->grants[$grant['id']]), "");

            }


        $WEB_APP['fields'] = $fields;

        $WEB_APP['view']->display('table.tpl', text('txt_users'));
    }

    function edit()
    {
        global $WEB_APP;
        global $adodb;
        $groups = get_groups('group_name');
        $grants = get_grants('grant_title');
        $WEB_APP['title'] = $WEB_APP['title_edit'];
        // After change press.
        $user = get_user($WEB_APP['id']);
        $user->password = '';
        $WEB_APP['errorstext'] .= $adodb->ErrorMsg();
        if (!isset($user->id))
        {
            redirect($WEB_APP['errorstext']);
        }
        if (($WEB_APP['id'] >= 0) && is_add_edit_form('name'))
        {
            $correct_post = TRUE;

            if ($_POST['group'] == 0)
            {
                $WEB_APP['errorstext'] .= text('txt_insert_group')."<br>";
                $correct_post = FALSE;
            }
            if (trim($_POST['login']) == "")
            {
                $WEB_APP['errorstext'] .= text('txt_insert_login')."<br>";
                $correct_post = FALSE;
            }
            /*if (trim($_POST['password']) == "")
            {
                $WEB_APP['errorstext'] .= text('txt_insert_password')."<br>";
                $correct_post = FALSE;
            }*/
            if (trim($_POST['name']) == "")
            {
                $WEB_APP['errorstext'] .= text('txt_insert_user_name')."<br>";
                $correct_post = FALSE;
            }

            if  (!(is_email($_POST['mail']) ||
                     ($_POST['mail'] == "")))
            {
                $WEB_APP['errorstext'] .= text('txt_email_does_not_correct_insert_correct_email')."<br>";
                $correct_post = FALSE;
            }

            $user_id = get_user_id($_POST['login']);
            if (!(  ($user_id == 0) ||
                ($user_id == $WEB_APP['id'])))
            {
                $WEB_APP['errorstext'] .= text('txt_user_already_exist_insert_another_user_login')."<br>";
                $correct_post = FALSE;
            }

            $user_id = get_user_id_by_mail($_POST['mail']);
            if (!(  ($user_id == 0) ||
                ($user_id == $WEB_APP['id'])))
            {
                $WEB_APP['errorstext'] .= text('txt_user_mail_already_exist_insert_another_user_mail')."<br>";
                $correct_post = FALSE;
            }

            if ($correct_post)
            {
                edit_user($WEB_APP['id'],
                     $_POST['group'],
                     $_POST['login'],
                     $_POST['password'],
                     $_POST['name'],
                     $_POST['info'],
                     $_POST['mail'],
                     isset($_POST['hidden']) ? 1 : 0,
                     isset($_POST['grants']) ? $_POST['grants'] : array());
                $WEB_APP['errorstext'] .= $adodb->ErrorMsg();
            }
            $user = $this->get_post_user();
            redirect($WEB_APP['errorstext']);
        }
        $WEB_APP['row_actions'] = array($WEB_APP['action_edit'], $WEB_APP['action_delete']);
        $WEB_APP['columns'] = array(new column('id', 'id'),
                new column("user_login", text('txt_login'), "login"),
                new column("group_name", text('txt_group'), "group"),
                new column("user_name", text('txt_user_name'), "name"),
                new column("user_info", text('txt_info'), "info"),
                new column("user_mail", text('txt_mail'), "mail"),
                new column("user_hidden", text('txt_hidden'), "hidden"));
        $WEB_APP['escape'] = TRUE;
        // Form fields.
        $fields = array();
        $fields[] = new field(  TRUE, text('txt_group'), "select", "group",
                        $user->group,"", $groups, "id", "group_name");
        $fields[] = new field(  TRUE, text('txt_login'), "text", "login",
                        $user->login, "");
        $fields[] = new field(  TRUE, text('txt_password'), "text", "password",
                    $user->password, "");
        $fields[] = new field(  TRUE, text('txt_user_name'), "text", "name",
                    $user->name, "");
        $fields[] = new field(  FALSE, text('txt_info'), "textarea", "info",
                    $user->info, "");
        $fields[] = new field(  FALSE, text('txt_mail'), "text", "mail",
                    $user->mail, "");
        $fields[] = new field(  FALSE, text('txt_hidden'), "checkbox", "hidden",
                        $user->hidden == 1, "");

        $tmp = 0;

            $fields[] = new field(  FALSE, text('txt_permissions'), "header");

            foreach($grants as $grant)
            {

                    $fields[] = new field(  FALSE, $grant['grant_title'], "checkbox",
                            "grants[".$grant['id']."]", isset($user->grants[$grant['id']]), "");

            }


        $WEB_APP['fields'] = $fields;

        $WEB_APP['view']->display('table.tpl', text('txt_users'));
    }
}

